package storage.data;

import Transaction.Transaction;
import Common.ByteArray;
import Common.GlobalConst;


/**
 * This is the first page of the whole database
 * it stores the number of pages and memory map 
 */
public class FirstPage extends HeaderPage {
	public static int NUM_OF_PAGE = 4 ;
	public int X_ENTRY_POINT = 8 ;
	public int NUM_ENTRY = ( GlobalConst.PAGESIZE - X_ENTRY_POINT ) / ENTRY_SIZE  ; 
	
	public FirstPage ( boolean needData) {
		super ( needData ) ;
	}
	
	public FirstPage ( Page apage ) {
		super ( false ) ;
		data = apage.getData() ; 
	}
		
	public int getNumDbPage ( ) {
		try{ 
			return ByteArray.readInt( data, NUM_OF_PAGE) ;
		}catch ( Exception e ) {
			return 2 ; 
		}
	}
	
	public int getNumMapPage ( ) {
		try{
			return ( ByteArray.readInt( data, NUM_OF_PAGE ) - 1 ) / GlobalConst.PAGESIZE / 8 + 1  ;
		}catch ( Exception e ) {
			return 1 ; 
		}
	}

	
	public void writeNumPage (Transaction tx, PageID pid, int noPages) {
		try {
			tx.set( defTable, pid, data, NUM_OF_PAGE, noPages, true) ; 
		}catch ( Exception e) {
			e.printStackTrace() ; 
		}
	}	
	
	protected int getEntryPoint () {
		return X_ENTRY_POINT ; 
	}
}


